#!/bin/bash
# Start Picraft minecraft server
# Backup server when stopped


# Get time via founction
Rd() {
  date +"%Y-%m-%d %H:%M:%S"
}


# LOG FILE CHECK START
logc=/home/pi/scripts/log.txt
if [ -f "$logc" ] ; then
  logfc=GOOD
else
  # Create logfile
  echo "#Log file" > /home/pi/scripts/log.txt
  # Log event
  echo "[$(Rd)] log.txt file not found!" >> /home/pi/scripts/log.txt
  echo "[$(Rd)] log.txt had been created by the script." >> /home/pi/scripts/log.txt
  # End Log event
fi

if [ -f "$logc" ] ; then
  logfc=GOOD
else
  logfc=BAD
fi
# END LOG FILE CHECK


# CONFIG FILE CHECK START
confc=/home/pi/scripts/picraft.conf
if [ -f "$confc" ] ; then
  conffc=GOOD
else
  echo "# This config file is only meat for picraft core script only" > /home/pi/scripts/picraft.conf
  echo "# Not related to the minecraft server file/script" >> /home/pi/scripts/picraft.conf
  echo "# Everything in here should not be change." >> /home/pi/scripts/picraft.conf
  echo "" >> /home/pi/scripts/picraft.conf
  # Log event
  echo "[$(Rd)] picraft.conf file not found!" >> /home/pi/scripts/log.txt
  echo "[$(Rd)] picraft.conf had been created by the script." >> /home/pi/scripts/log.txt
  # End Log event
fi

if [ -f "$confc" ] ; then
  conffc=GOOD
else
  conffc=BAD
fi
# END CONFIG FILE CHECK


# Link colour table
source /home/pi/scripts/colourtable.sh

# Link config file
source /home/pi/scripts/picraft.conf


# DEFINE(CREATE DIF) VARIABLES
# Dependency check $dpc
if [ -f ${dpc} ] ; then
echo "dpc=false" >> /home/pi/scripts/picraft.conf
fi
# END DEFINE(CREATE DIF) VARIABLES


# LOAD DISPLAY
clear
echo ""
echo ""
echo " ______   __     ______     ______     ______     ______   ______ " 
echo "/\  == \ /\ \   /\  ___\   /\  == \   /\  __ \   /\  ___\ /\__  _\ "
echo "\ \  _-/ \ \ \  \ \ \____  \ \  __<   \ \  __ \  \ \  __\ \/_/\ \/ "
echo " \ \_\    \ \_\  \ \_____\  \ \_\ \_\  \ \_\ \_\  \ \_\      \ \_\ "
echo "  \/_/     \/_/   \/_____/   \/_/ /_/   \/_/\/_/   \/_/       \/_/ "
echo ""
echo ""
echo -e "${LGREEN}          XXXXXXXXXXXXXXXXXXXXXXXX   ${NC} ${GREEN}Server version : 1.16.1 ${NC}"
echo -e "${LGREEN}       XXXX   X              XXX X   ${NC} Based on Papermc-135.jar"
echo -e "${LGREEN}     XXX            X       XX   X   ${NC} "
echo -e "${LGREEN}   XXX    X       X       XX     X   ${NC} Server time: ${GREEN}$(Rd)${NC}"
echo -e "${LGREEN} XXX                    XXX    XXX   ${NC} Log file: ${GREEN}$logfc${NC}"
echo -e "${LGREEN}XXXXXXXXXXXXXXXXXXXXXXXXX     XX X   ${NC} Config File: ${GREEN}$conffc${NC}"
echo -e "${LGREEN} X X                    X   XX   X   ${NC} Raspberry Pi 4B"
echo -e "${LGREEN} X        X    X        X XX     X   ${NC} 8Gib Ram 128Gib Storage"
echo -e "${LGREEN} XXXXXXX XXXX XXXXXXXXXXXX    X  X   ${NC} Scripts by Ketchup Putty"
echo -e "${ORANGE} X  XX XXX  XX    XX    X        X   ${NC} "
echo -e "${ORANGE} X   X                  X  X     X   ${NC} ${LG}Uder LGPL-3.0 License ${NC}"
echo -e "${ORANGE} X                      X        X   ${NC} Gitee repository"
echo -e "${ORANGE} X       X      X       X      XXX   ${NC} ${LG}ketchupputty/picraft... ${NC}"
echo -e "${ORANGE} X X                    X    XXX     ${NC} "
echo -e "${ORANGE} X    X        X   X    X   XX       ${NC} ${CYAN}Main Script version : 1.0 ${NC}"
echo -e "${ORANGE} X           X          X XX         ${NC} "
echo -e "${ORANGE} XXXXXXXXXXXXXXXXXXXXXXXXX           ${NC} "
echo ""
# Log event
echo "[$(Rd)] ==============Cycle================" >> /home/pi/scripts/log.txt
echo "[$(Rd)] Picraft server scritp started!" >> /home/pi/scripts/log.txt
# End Log event
# END LOAD DISPLAY

sleep 1

# CHECK LOG FILE UI
echo ""
echo -e "====${GREEN}Picraft Starter${NC}================================================"
echo ""
echo "                        Checking Log file                          "
echo ""
echo "==================================================================="
echo ""
sleep 1

# If log file is not created, stop runing
if [ $logfc != "GOOD" ] ; then
echo ""
echo -e "====${RED}Picraft Starter${NC}================================================"
echo ""
echo "            Fatal error! log file cannot be created!               "
echo "                Please create log file manually.                   "
echo ""
echo "==================================================================="
echo ""
exit 1
else
echo ""
echo -e "====${GREEN}Picraft Starter${NC}================================================"
echo ""
echo "                     log.txt file is valid                         "
echo ""
echo "==================================================================="
echo ""
# Log event
echo "[$(Rd)] Log file checked and passed." >> /home/pi/scripts/log.txt
# End Log event
sleep 1
fi
# END CHECK LOG FILE UI

sleep 1

# CHECK CONFIG FILE UI
echo ""
echo -e "====${GREEN}Picraft Starter${NC}================================================"
echo ""
echo "                      Checking config file                         "
echo ""
echo "==================================================================="
echo ""
sleep 1

# If log file is not created, stop runing
if [ $conffc != "GOOD" ] ; then
echo ""
echo -e "====${RED}Picraft Starter${NC}================================================"
echo ""
echo "        Fatal error! picraft.conf file cannot be created!          "
echo "               Please create config file manually.                 "
echo ""
echo "==================================================================="
echo ""
# Log event
echo "[$(Rd)] picraft.conf cannot be created or unable to gain access, check your permissions." >> /home/pi/scripts/log.txt
# End Log event
exit 1
else
echo ""
echo -e "====${GREEN}Picraft Starter${NC}================================================"
echo ""
echo "                   picraft.conf file is valid                      "
echo ""
echo "==================================================================="
echo ""
# Log event
echo "[$(Rd)] picraft config  file checked and passed." >> /home/pi/scripts/log.txt
# End Log event
sleep 1
fi
# END CHECK CONFIG FILE UI

sleep 1

# DEPENDENCIES CHECK
echo ""
echo -e "====${GREEN}Picraft Starter${NC}================================================"
echo ""
echo "                     Checking denpendencies                        "
echo ""
echo "==================================================================="
echo ""
sleep 1

# Check config file see if it true
if [ "${dpc}" != "true" ]
then
echo ""
echo -e "===${ORANGE}Dpc Checker${NC}====================================================="
echo ""
sudo bash /home/pi/scripts/dependency_check.sh
echo ""
echo "==================================================================="
echo ""
sed -i -e '/^dpc=/s|.*|dpc=true|' /home/pi/scripts/picraft.conf
else
echo ""
echo -e "===${ORANGE}Dpc Checker${NC}====================================================="
echo ""
echo -e "                      ${GREEN}Everything looks fine.${NC}                        "
echo ""
echo "===================================================================="
echo ""
fi
# END DEPENDENCIES CHECK


cd /home/pi/server

sleep 1

# SERVER START SCRIPT
bash start.sh
# END SERVER START SCRIPT

sleep 1

cd /home/pi/scripts

sleep 1

#clear

# SERVER STOP NOTIFICATION
echo ""
echo -e "====${GREEN}Picraft Starter${NC}================================================"
echo ""
echo -e "                          ${RED}Server stopped${NC}                           "
echo ""
echo "==================================================================="
echo ""
# Log event
echo "[$(Rd)] Server stopped." >> /home/pi/scripts/log.txt
# End Log event
# END SERVER STOP NOTIFICATION

sleep 2

#clear

# SERVER BACKUP
echo ""
echo -e "===${LGREEN}Server Backup${NC}==================================================="
echo ""
echo "                 Do you want to backup the server?                 "
echo ""
echo "==================================================================="
echo -e "              ${ORANGE}Entre or wiat for 10 seconds auto backup${NC}             "
echo "==================================================================="
echo""
# Log event
echo "[$(Rd)] Wait for backup confirmation timeout, auto backup started." >> /home/pi/scripts/log.txt
# End Log event
read -t 10 -p 'Yes or No: ' YON

if [ -z "$YON" ]
then
  echo ""
  echo -e "===${LGREEN}Server Backup${NC}==================================================="
  echo ""
  echo "               Your time is up. Auto backup started!               "
  echo ""
  echo "==================================================================="
  # Log event
  echo "[$(Rd)] Wait for backup confirmation timeout, auto backup started." >> /home/pi/scripts/log.txt
  # End Log event
  sleep 1
  YON=Yes
fi

if [ $YON == 'Yes' ] || [  $YON == 'YES' ] || [ $YON == 'yes' ] || [ $YON == 'Y' ] || [ $YON == 'y' ]
then
  echo ""
  echo -e "===${LGREEN}Server Backup${NC}==================================================="
  echo ""
  echo "                   Confirmed, Getting ready                        "
  echo ""
  echo "==================================================================="
  echo ""
  # Log event
  echo "[$(Rd)] Backup confirmation confirmed, backup started." >> /home/pi/scripts/log.txt
  # End Log event

elif [ $YON == 'No' ] || [ $YON == 'NO' ] || [ $YON == 'no' ] || [ $YON == 'N' ] || [ $YON == 'n' ]
then
  echo ""
  echo -e "===${LGREEN}Server Backup${NC}==================================================="
  echo ""
  echo "                        Backup canceled!                           "
  echo "            You will need to manually start the server.            "
  echo ""
  echo "==================================================================="
  echo ""
  # Log event
  echo "[$(Rd)] Backup confirmation confirmed, backup canceled." >> /home/pi/scripts/log.txt
  # End Log event
  exit 0
else
  echo ""
  echo -e "===${LGREEN}Server Backup${NC}==================================================="
  echo "                   $YON"
  echo "                   Invild input! Backup skipped                    "
  echo ""
  echo "==================================================================="
  echo ""
  # Log event
  echo "[$(Rd)] Backup confirmation return invild input: $YON, backup dropped." >> /home/pi/scripts/log.txt
  # End Log event
  exit 1
fi

echo -e "===${LGREEN}Server Backup${NC}==================================================="
echo ""
sudo bash /home/pi/scripts/backup_picraft.sh
echo ""
echo "==================================================================="
echo ""

echo -e "===${LGREEN}Server Backup${NC}==================================================="
echo ""
echo -e "                    ${GREEN}Server Backup completed ${NC}                       "
echo ""
echo "==================================================================="
echo ""
# Log event
echo "[$(Rd)] Backup completed." >> /home/pi/scripts/log.txt
# End Log event
# END SERVER BACKUP
exit 0